/*
 * Created on 2006-2-20
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.hitrust.gs.shipment;


import com.hitrust.gs.util.exception.*;
import com.hitrust.gs.lib.GSLogWriter;
import com.hitrust.gs.lib.GSSystemConfigurator;
import com.hitrust.gs.util.date.*;
/**
 * @author simonyi
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class GSShipmentFlowManager extends Thread {

    private Download download = new Download();
    private GSShipmentManager manager = new GSShipmentManager();
    
    private boolean isDownSuccess = false;
    private boolean isExecSuccess = false;
    private String lastDate = "";
    
    private boolean processResult = false;//当日处理结果
    
    private static GSShipmentFlowManager fm = new GSShipmentFlowManager();
    
    public static GSShipmentFlowManager getInstance(){
        return fm;
    }
    
    private GSShipmentFlowManager(){

        System.out.println("\n*****XXXXX GSShipmentFlowManager is running XXXXX*****\n");
        start();
    
    }
    
    public void run(){
        try{
            while(true){
                String currTime = DateUtil.getCurrentTime("T","AD");
                String currDate = GSShipmentConstants.getCurrentDate();
                System.out.println("currTime:"+currTime);
                System.out.println("currDate:"+currDate);
                //如果当日已经执行过操作
                if(currDate.equals(lastDate) && processResult){
                    System.out.println("Today's shipment Oper has been executed...");
                    sleep(1000 * 60 * 60 * 4);//休眠4 * 60分钟
                    //sleep(1000 * 60 * 30);//休眠30分钟 测试用 2008-01-31 by Sindy
                }
                else{
                    //超过指定的处理时间
                    if(Integer.parseInt(currTime) > Integer.parseInt(GSSystemConfigurator.getParam("gs.shipment.processtime"))){
                        System.out.println("It's begin time to execute sipment Oper...");
                        //并且没有超过指定的结束时间
                        if(Integer.parseInt(currTime) < Integer.parseInt(GSSystemConfigurator.getParam("gs.shipment.endtime"))){
                            System.out.println("executing...on " + currTime);
                            //更新系统记录的当日操作的处理时间
                            lastDate = currDate;
                            processResult = false;
                            //执行动作
                            //processResult = execute(); 2008-01-31 by Sindy
                        }
                        //如果超过了指定的结束时间系统将不再执行
                        else{
                            System.out.println("But it's behind the time of end time ...");
                        }
                    }
                    sleep(30 * 60 * 1000);//处理完毕，休眠30分钟，用于在下载不成功时的缓冲
                    //sleep(10 * 60 * 1000);//休眠10分钟 测试用 2008-01-31 by Sindy
                }
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    
    public boolean execute(){

        String date="";//"20060606";
        boolean returnValue = false;
        
        try{
           date = GSShipmentConstants.getYesterday();
            GSLogWriter.info("Shipment Time:" + date);
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
        }
        
        //3.0: 根据对帐日期察看执行状态
        try{
            returnValue = manager.isProcessSucceed(date);
            if(returnValue) return returnValue;
        }
        catch(GSException e){
            GSLogWriter.error(this,e.toString());
        }
        //3.1: 根据当前对帐日期下载出货单
        try{
             download.transactionShipmentFile(date);
            isDownSuccess =true;
        }catch(GSException e){
            GSLogWriter.error(this,e.toString());
        }
        //isDownSuccess =true;

        //4.1: 对帐ALLITEMS
        if(isDownSuccess){
            try{
                isExecSuccess = manager.saveShipment(GSShipmentConstants.getFile(date),date);

            }catch(GSException e){
                GSLogWriter.error(this,e.getMessage());    
            }            
        }

        returnValue = isDownSuccess && isExecSuccess;
        return returnValue;
        
    }
    
    public static void main(String[] args){
        GSShipmentFlowManager m = GSShipmentFlowManager.getInstance();
        //m.aa();
        //m.execute();
        
        String currTime = DateUtil.getCurrentTime("T","AD");
        System.out.println(currTime);
        
    }
}
